package yxy.game.pm2.active.command.monthcard;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_月卡;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.ActiveMessage.ActMonthCardDrawRewardRequest;
import yxy.game.pm2.message.PackageMessage;

/**
 * 领取月卡每日奖励
 * @author Dell
 *
 */
@MessageType(proto = Command.ActMonthCardDrawReward, classtype = ActiveMessage.ActMonthCardDrawRewardRequest.class)
public class CMD_Active_月卡_DrawReward extends Command<ActMonthCardDrawRewardRequest> {

	@Override
	public void execute(MessageSource messageSource, ActMonthCardDrawRewardRequest request) throws Exception {
		String pcode = messageSource.getTag();
		ActiveMessage.ActMonthCardDrawRewardResponse.Builder response = ActiveMessage.ActMonthCardDrawRewardResponse.newBuilder();
		int group = request.getGroup();
		
		ActiveModule_月卡 am = ActiveModule_月卡.get(pcode,false);
		if(am == null) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		int state = am.canmcreward(group);
		if(state == DrawStatus.cannotget.getStatus()) {
			send(messageSource, response, Result.ActCannotGet);
			return;
		}
		if(state == DrawStatus.hadget.getStatus()) {
			send(messageSource, response, Result.ActHadGet);
			return;
		}
		
		Currency cur = am.mcreward(group);
		if(cur == null) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		am.handledrawreward(group, cur);
		
		PackageMessage.Item.Builder item = PackageMessage.Item.newBuilder();
		
		item.setType(cur.getType().getNumber());
		item.setDataId(cur.getDataId());
		item.setNumber(cur.getNumber());
		response.addDisplay(item.build());
		response.addUpdate(item.build());
		
		send(messageSource, response, Result.Success);
	}

	
	private void send(MessageSource messageSource, ActiveMessage.ActMonthCardDrawRewardResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
	
}
